﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO.IsolatedStorage;

namespace QuickWeb
{
    class AppSettings
    {
        IsolatedStorageSettings isolatedStore;
        const string selectedSearchEngineKeyName = "SearchEngineSetting";
        const string selectedStartPivotKeyName = "StartPivotSetting";

        //El motor de busqueda seleccionado
        //0-> DuckDuckGo
        //1-> Google
        //2-> Bing
        const int SearchEngineSettingDefault = 0;
        //El pivot de inicio
        //0->Search
        //1->Read
        const int StartPivotSettingDefault = 0;
        public AppSettings() { 
            //try
            //{
            //    // Get the settings for this application.
            //    isolatedStore = IsolatedStorageSettings.ApplicationSettings;
            //}
            //catch (Exception e)
            //{
            //    System.Diagnostics.Debug.WriteLine("Exception while using IsolatedStorageSettings: " + e.ToString());
            //}
        }


        public bool AddOrUpdateValue(string Key, Object value)
        {
            bool valueChanged = false;

            // If the key exists
            if (isolatedStore.Contains(Key))
            {
                // If the value has changed
                if (isolatedStore[Key] != value)
                {
                    // Store the new value
                    isolatedStore[Key] = value;
                    valueChanged = true;
                }
            }
            // Otherwise create the key.
            else
            {
                isolatedStore.Add(Key, value);
                valueChanged = true;
            }
            return valueChanged;
        }

        public valueType GetValueOrDefault<valueType>(string Key, valueType defaultValue)
        {
            valueType value;

            // If the key exists, retrieve the value.
            if (isolatedStore.Contains(Key))
            {                
                 value = (valueType)isolatedStore[Key];               
            }
            // Otherwise, use the default value.
            else
            {
                value = defaultValue;
            }
            return value;
        }

        public void SaveSettings()
        {
            isolatedStore.Save();
        }

        

    }
}
